<?php

namespace App\Http\Controllers\Client;

use App\Exceptions\GeneralException;
use App\Http\Requests\Client\Work\WorkCode;
use App\Http\Requests\Client\Work\WorkExpress;
use App\Http\Requests\Client\Work\WorkSend;
use Illuminate\Support\Facades\Auth;
use App\Repositories\OrderRepository;
use App\Http\Controllers\Controller;
use App\Http\Requests\Client\Work\QueryResource;
use App\Http\Requests\Client\Work\WorkCreate;
use App\Http\Requests\Client\Work\WorkUpdate;
use App\Http\Requests\Client\Work\WorkDelivery;
use App\Http\Requests\Client\Work\WorkSign;

class WorkController extends Controller
{
    /**
     *  ORDER_SIGN_LISTS 订单标签列表
     */
    const ORDER_SIGN_LISTS = [
        '1' => '代收货款',
        '2' => '污染',
        '3' => '破损',
        '4' => '拒收',
        '5' => '错分',
        '6' => '地址变更',
    ];

    protected $orderRepository;

    /**
     * WorksController constructor.
     * @param OrderRepository $orderRepository
     */
    public function __construct(
        OrderRepository $orderRepository
    )
    {
        $this->orderRepository = $orderRepository;
    }

    /**
     * 获取派件列表资源
     *
     * Date: 2018/05/25
     * @author Tenny
     * @param QueryResource $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function lists(QueryResource $request)
    {
        $user = Auth::guard('courier')->user();
        $courier_id = $user->id;
        $service_id = $user->service_id;
        $type = $request->get('type', false);
        $status = 2;
        $created_at = $request->get('created_at', false);
        $limit = $request->get('paginate', 15);
        if ($created_at === null) {
            $created_at = false;
        }
        $works = $this->orderRepository->getOrders($courier_id, $service_id, $type, $status, $created_at, $limit);
        return success($works);
    }

    /**
     * 标记
     *
     * Date: 2018/05/29
     * @author Tenny
     * @param WorkSign $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\GeneralException
     */
    public function sign(WorkSign $request)
    {
        $sign = $request->get('sign', 0);
        $order_id = $request->get('order_id', false);
        $region_mark = $request->get('region_mark', false);
        $ip = $request->getClientIp();
        $label = '';
        if (is_array($sign)) {
            $sign_lists = $sign;
        } elseif (json_decode($sign)) {
            $sign_lists = json_decode($sign);
        } else {
            throw new GeneralException('标签格式不正确', 422);
        }
        if (is_array($sign_lists) && count($sign_lists) > 0) {
            foreach ($sign_lists as $value) {
                if (self::ORDER_SIGN_LISTS[$value]) {
                    $label .= ',' . self::ORDER_SIGN_LISTS[$value];
                }
            }
        } else {
            throw new GeneralException('标签格式不正确', 422);
        }
        $label = trim($label, ',');
        if (mb_strlen($label) < 1) {
            throw new GeneralException('标签格式不正确', 422);
        }
        $works = $this->orderRepository->doSign($order_id, $label, $ip, $region_mark);
        return success($works);
    }


    /**
     * 签收
     *
     * Date: 2018/05/25
     * @author Tenny
     * @param WorkDelivery $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\GeneralException
     */
    public function signing(WorkDelivery $request)
    {
        $order_id = $request->get('order_id', false);
        $single_number = $request->get('single_number', false);
        $ip = $request->getClientIp();
        $works = $this->orderRepository->doSigning($order_id, $single_number, $ip);
        return success($works);
    }

    /**
     * 入库
     *
     * Date: 2018/05/25
     * @author Tenny
     * @param WorkCreate $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\GeneralException
     */
    public function entry(WorkCreate $request)
    {
        $works = $this->orderRepository->addOrder_v2($request);
        return success($works);
    }

    /**
     * 出库
     *
     * Date: 2018/05/25
     * @author Tenny
     * @param WorkDelivery $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\GeneralException
     */
    public function delivery(WorkDelivery $request)
    {
        $order_id = $request->get('order_id', false);
        $single_number = $request->get('single_number', false);
        $ip = $request->getClientIp();
        $works = $this->orderRepository->doSigning($order_id, $single_number, $ip);
        return success($works);
    }

    /**
     * 派送
     *
     * Date: 2018/05/25
     * @author Tenny
     * @param WorkSend $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\GeneralException
     */
    public function send(WorkSend $request)
    {
        $order_id = $request->get('order_id', false);
        $single_number = $request->get('single_number', false);
        $ip = $request->getClientIp();
        $works = $this->orderRepository->doSend($order_id, $single_number, $ip);
        return success($works);
    }

    /**
     * 派送列表
     *
     * Date: 2018/05/25
     * @author Tenny
     * @param QueryResource $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function sendLists(QueryResource $request)
    {
        $user = Auth::guard('courier')->user();
        $courier_id = $user->id;
        $service_id = $user->service_id;
        $type = 0;
        /*
         *  $mold    1:进行中的订单  2:已完成订单  3:异常订单  默认进行中的订单
         * */
        $mold = $request->get('mold', 1);
        $limit = $request->get('paginate', 15);
        $works = $this->orderRepository->getPersonalOrders($courier_id, $service_id, $type, $mold, $limit);
        return success($works);
    }

    /**
     * 自提订单列表
     *
     * Date: 2018/05/25
     * @author Tenny
     * @param QueryResource $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function lifting(QueryResource $request)
    {
        $user = Auth::guard('courier')->user();
        $courier_id = $user->id;
        $service_id = $user->service_id;
        $type = 1;
        /*
         *  $mold    1:进行中的订单  2:已完成订单  3:异常订单  默认进行中的订单
         * */
        $mold = $request->get('mold', 1);
        $limit = $request->get('paginate', 15);
        $works = $this->orderRepository->getPersonalOrders($courier_id, $service_id, $type, $mold, $limit);
        return success($works);
    }

    /**
     * 派送
     *
     * Date: 2018/05/25
     * @author Tenny
     * @param WorkUpdate $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function detail(WorkUpdate $request)
    {
        $order_id = $request->get('order_id', false);
        $orders = $this->orderRepository->getOrderDetails($order_id);
        return success($orders);
    }

    /**
     * 获取物流公司
     *
     * Date: 2018/05/25
     * @author Tenny
     * @param WorkExpress $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\GeneralException
     */
    public function express(WorkExpress $request)
    {
        $user = Auth::guard('courier')->user();
        $service_id = $user->service_id;
        $order_id = $request->get('single_number', false);
        $orders = $this->orderRepository->getExpress($order_id);
        $orders['take_code'] = $this->orderRepository->getTakeCode($service_id);
        return success($orders);
    }

    /**
     * 获取取件码
     *
     * Date: 2018/05/25
     * @author Tenny
     * @param WorkCode $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function takeCode(WorkCode $request)
    {
        $user = Auth::guard('courier')->user();
        $service_id = $user->service_id;
        $code = $this->orderRepository->getTakeCode($service_id);
        return success(['code' => $code]);
    }
}
